package es.uji.viselab.image.source;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import es.uji.viselab.experiment.ExperimentParameters;
import es.uji.viselab.robot.Robot;

public class URLCamera extends ImageSource implements ActionListener {

	JPanel rootPanel;
	protected static final int maximumCornersSaved = 5;
	protected Label labelURL = new Label("URL: ");
	protected TextField textURL = new TextField(30);
	protected Label labelFrecuency = new Label("Times per second: ");
	protected TextField textFrecuency = new TextField(5);

	protected Button buttonOpenCamera = new Button("Connect");
	protected Timer timer = new Timer();
	protected Double[][] lastCorners = null;
	protected double[] corners;
	BufferedImage bImage = null;

	public URLCamera(ExperimentParameters ep) {
		super(ep);
		lastCorners = new Double[maximumCornersSaved][8];
		constructPanel();
	}

	protected void constructPanel() {

		rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));

		textURL.setText("http://150.128.86.214:8080/cam_1.jpg");
		// textURL.setText("http://localhost:10100/");
		textFrecuency.setText("5");

		JPanel line0 = new JPanel();
		line0.setLayout(new FlowLayout());
		line0.add(labelURL);
		line0.add(textURL);
		line0.add(labelFrecuency);
		line0.add(textFrecuency);
		line0.add(buttonOpenCamera);
		rootPanel.add(line0);

		buttonOpenCamera.addActionListener(this);
	}

	public JPanel getPanel() {

		return rootPanel;
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(buttonOpenCamera)) {
			double fraction = 1.0 / Long.parseLong(textFrecuency.getText());
			long miliseconds = (long) (fraction * 1000);
			Timer timer = new Timer();
			timer.schedule(this, (long) 0.0, miliseconds);
		}
	}

	@Override
	public void run() {
		redrawImage();
	}

	private void drawCorners() {

		Graphics g = rootPanel.getGraphics();

		g.setColor(Color.RED);
		if (imagePointList != null) {
			for (int i = 0; i < 3; i++) {
				g.drawLine((int) imagePointList[i].x, (int) imagePointList[i].y + 50,
						(int) imagePointList[i + 1].x, (int) imagePointList[i + 1].y + 50);
			}
			g.drawLine((int) imagePointList[3].x, (int) imagePointList[3].y + 50,
					(int) imagePointList[0].x, (int) imagePointList[0].y + 50);
			for (int i = 0; i < 4; i++) {
				g.drawOval((int) imagePointList[i].x - 3, (int) imagePointList[i].y + 50 - 3, 6, 6);
			}
		}

		g.setColor(Color.green);
		if (targetPointList != null) {

			for (int i = 0; i < 4; i++) {
				g.drawOval((int) targetPointList[i].x - 3, (int) targetPointList[i].y + 50 - 3, 6,
						6);
			}
		}
	}

	private void redrawImage() {
		// redrawFromObjectFinder2();
		getImage();
		if (processor != null)
			imagePointList = processor.process(bImage);

		if (imagePointList != null) {
			drawCorners();
			imageCoordinateList = convertUVtoXY(imagePointList);
			// System.out.println(imagePointList[0].x + " " +
			// imagePointList[0].y);
			// System.out.println(imageCoordinateList[0].x + " " +
			// imageCoordinateList[0].y);
		}
	}

	private void getImage() {
		URL url = null;
		try {
			// url = new URL("http://localhost:10100/");
			// url = new URL("http://150.128.87:8080/cam_1.jpg");
			url = new URL(textURL.getText());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			bImage = ImageIO.read(url);
			Graphics g = rootPanel.getGraphics();
			boolean drawImage = g.drawImage(bImage, 0, 50, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// this.camParams.setImageHeight(bImage.getHeight());
		// this.camParams.setImageWidth(bImage.getWidth());
	}

	@Override
	public void robotPositionChanged(Robot robot) {
		redrawImage();
		notifyFeaturePointsChange(this);
	}

	@Override
	public void robotCreated(Robot robot) {
		// TODO Auto-generated method stub

	}

	@Override
	public void robotConnected(Robot robot) {
		redrawImage();
		notifyFeaturePointsChange(this);

	}

	@Override
	public void robotDisconnected() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getPanelTitle() {
		return "URL camera";
	}

	@Override
	public int panelWidth() {
		return 640;
	}

	@Override
	public int panelHeight() {
		return 480 + 70;
	}
}
